<?PHP
/*  stu_fitness_ques  v0.1
 *  Plugin FOR Discuz! X 
 *	By STU Michael && NULL
 */
 
 
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

require_once DISCUZ_ROOT.'./source/function/function_discuzcode.php';
	//submitcheck判断是否是远程提交
	if(!submitcheck('newquessubmit')) {
      	//if(empty($_G['uid']) ||!in_array($_G['member']['groupid'],$allowpost)) showmessage('请先登录再新建问卷' , 'plugin.php?id=stu_fitness:stu_fitness' ,'',array('alert' => 'error'));
	    if(empty($_G['uid'])) showmessage('请先登录再新建问卷' , 'plugin.php?id=stu_fitness:stu_fitness' ,'',array('alert' => 'error'));
	    //$mischecktm =  lang('plugin/nds_up_ques', 'checktm');
	    $mischecktm = "请检查基本信息填写是否完整，没有填写名称的项目将被过滤。确认提交？";
	}else{
		$subject 		= $_POST['subject'];	//问卷标题
		$quesclass 		= $_POST['quesclass'];	//问卷类型
		$exp 			= (int)$_POST['exp'];		//问卷有效天数
		$message		= $_POST['message'];	//问卷说明
		//把问卷写入数据
		$tdata = array(
	        'title' => $subject,
	        'author_id' =>  $_G[uid],
	        'post_time' => strtotime("now"),
	        'deadline' =>  strtotime('+'.$exp.' days'),
	        'submit_count' => 0 , 
	        'status' => 1, 
	        'type_id' => 0,
	        'comment' => $message
       ); 
	  	//$_G['tablepre'].
        $Qnire_id  = DB::insert('fitness_questionnaire', $tdata, true);	
		
		
		$questionList   = array();				//问题列表
		$questionCount  = count($_POST['title']);//问题数量
		$optionList		= array();				//选项列表
		$orderList		= array();				//存放顺序的列表,在排序的时候用到（最为对比参数）
		
		echo $questionCount."questionCount<br/>";
		for($i = 0; $i < $questionCount ; $i++){
			//题目顺序
			if($_GET['order'][$i]!=NULL){
				$questionList[$i]['order']	= (int)$_GET['order'][$i];	
			}else{
				$questionList[$i]['order'] 	= 0;
			}
			
			// print_r($_GET['options']);
			// echo "Get<br/>";
			$questionList[$i]['title']		= $_GET['title'][$i];		//题目的问题内容
			$questionList[$i]['least']		= (int)$_GET['least'][$i];	//题目顺序 0为必答 1为选答
			$questionList[$i]['types']		= $_GET['types'][$i];		//题目类型问题类型 
			$questionList[$i]['options']	= $_GET['options'][$i];		//题目选项
			$questionList[$i]['min_choose']	= $_GET['minNum'][$i];			//题目选项
			$questionList[$i]['max_choose']	= $_GET['maxNum'][$i];			//题目选项
			$orderList[$i]				= $questionList[$i]['order'];	//追加到顺序列表
			
			// print_r($_GET['maxNum']);
			// echo "Get maxNum<br/>";
			
		}
		
		// print_r($questionList);
		// echo "pre array_multisort<br/>";
		//对问题列表按照order字段来排序
		array_multisort($questionList,SORT_NUMERIC,$orderList[$i]);
		
		// print_r($questionList);
		// echo "after array_multisort<br/>";
		foreach ($questionList as $key=>$value)
		{
			$questionList[$key]['order'] 	= (int)$key;
			echo "order".$questionList[$key]['order'];
			echo "<br/>";
			//把问题写入数据库
			$qdata = array(
		        'title' 		=> $questionList[$key]['title'],
		        'required' 		=> $questionList[$key]['least'],
		        'question_type' => $questionList[$key]['types'],
		        'questionnaire_id' =>$Qnire_id , 
		        'order' 		=> $questionList[$key]['order'],
		        'min_choose' 	=> $questionList[$key]['min_choose'],
		        'max_choose' 	=> $questionList[$key]['max_choose'],
		    ); 
			// print_r($questionList[$key]['minNum']);
			// echo "Get<br/>";
        	$Q_id  = DB::insert('fitness_question', $qdata, true);	
			print_r($questionList[$key]['types']);
			echo "questionList_option<br/>";
			//1为普通单选 2为普通多选 3为纯字符填空 4为学校 5为省份 6为日期 7为数字填空
			if(($questionList[$key]['types'] == '1')||($questionList[$key]['types'] == '2')){
				//showmessage($questionList[$key]['options'] , 'plugin.php?id=nds_up_ques&module=stu_fitness&action=view&topicid='.$topicid.'','');
				print_r($questionList[$key]['options']);
				echo "questionList_option<br/>";
				$optionList = explode("\n",$questionList[$key]['options']);
				print_r($optionList);
				echo "optionList<br/>";
				foreach ($optionList as $Okey => $Ovalue) {
					$odata = array(
				        'content' 		=> $Ovalue,
				        'question_id' 	=> $Q_id
				    ); 
					DB::insert('fitness_option', $odata, true);	
				}
			}else{
				$odata = array(
				        'content' 		=> $questionList[$key]['options'],
				        'question_id' 	=> $Q_id
				); 
				DB::insert('fitness_option', $odata, true);	
			}
			//showmessage('问卷发布成功，现在返回问卷列表' , 'plugin.php?id=nds_up_ques&module=stu_fitness&action=view&topicid='.$topicid.'','');
			
			
		}

		showmessage('问卷发布成功，现在返回问卷列表' , 'plugin.php?id=stu_fitness&action=newques');
		
    }
     //$navtitle = lang('plugin/stu_fitness', 'action_2').' - '.$navtitle;
	 $navtitle = '发表新问卷'.' - '.$navtitle;
     include template('stu_fitness:fitness_newques');
	
